package com.example.kuaizu.controller;

import com.example.kuaizu.pojo.PageBean;
import com.example.kuaizu.pojo.SubMassageParam;
import com.example.kuaizu.pojo.dto.ApplicationProjectListDTO;
import com.example.kuaizu.pojo.ov.ProjectOv;
import com.example.kuaizu.pojo.pram.*;
import com.example.kuaizu.service.InProjectService;
import com.example.kuaizu.service.InSubscribeMassageService;
import com.example.kuaizu.utils.AjaxResponse;
import com.example.kuaizu.utils.ThreadLocalUtil;
import kuaizu.common.PageData;
import kuaizu.common.groups.Create;
import kuaizu.common.param.QueryDataPoJectInfoPram;
import kuaizu.common.tablepojo.Project;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiaokai
 */
@Log4j2
@RestController
@RequestMapping("/project")
public class ProjectController {
    @Autowired
    InProjectService inProjectService;
    @Autowired
    private InSubscribeMassageService inSubscribeMassageService;

    @GetMapping("/tes")
    public void test() {
        inProjectService.test();
    }

    //创建项目
    @PostMapping("/create")
    public AjaxResponse createProject(@RequestBody @Validated(Create.class) CreateProjectPram req) {
        Project pro = new Project();
        BeanUtils.copyProperties(req, pro);
        Project project = inProjectService.createProject(pro);
        if (null != req.getIsAuditStatus() && req.getIsAuditStatus()) {
            //项目审核通知
            log.info("项目审核通知");
            SubMassageParam sp = new SubMassageParam();
            sp.setProjectId(project.getId());
            sp.setPageUrl(req.getAuditPageUrl());
            //审核
            inSubscribeMassageService.shenHeMassage(sp);
        }

        //判断是否接受项目投递通知
        if (null != req.getIsDeliveryStatus() && req.getIsDeliveryStatus()) {
            log.info("项目订阅通知");
            SubMassageParam sp = new SubMassageParam();
            sp.setProjectId(project.getId());
            sp.setPageUrl(req.getDeliveryPageUrl());
            inSubscribeMassageService.subscribe(sp);

        }

        //创建项目默认加入到项目中去
        //ProjectJoin projectJoin = new ProjectJoin();
        //
        //inProjectService.joinProject(projectJoin);
        return AjaxResponse.success("项目创建成功", project);
    }

    //加入项目
    @PostMapping("/join/{id}")
    public AjaxResponse joinProject(@PathVariable Integer id, @RequestBody JoinSubscribeMassageParam req) {
        //System.out.println("项目id" + id);
        log.info("项目id:{}", id);
        inProjectService.joinProject(id);
        if (req.isStatus()) {
            //加入项目的订阅消息
            req.setProjectId(id);
            Map<String, Object> user = ThreadLocalUtil.get();
            req.setOpenId(user.get("openid").toString());
            inSubscribeMassageService.JoinMassageSubscribe(req);
        }
        return AjaxResponse.success("项目加入成功");
    }

    //查询自己的项目信息
    @GetMapping("/projectinfo")
    public AjaxResponse getProjectInfo() {
        //获取用户信息
        Map<String, Object> user = ThreadLocalUtil.get();
        List<ProjectOv> list = inProjectService.SelectProjectInfo(user.get("openid").toString());
        return AjaxResponse.success("项目信息查询成功", list);
    }

    //假如项目申请审核
    @PostMapping("/approve")
    public AjaxResponse approveProject(
            //参数不能为空,也不能为null
            Integer projectid,
            String id,
            Integer status) {
        log.info("项目id:{}", projectid);
        log.info("用户openid:{}", id);
        log.info("状态:{}", status);
        if (inProjectService.UpdateProjectJoin(projectid, id, status) == 0) {
            return AjaxResponse.error("项目审核失败,请检查项目id和openid1是否对应起来");
        }

        return AjaxResponse.success("项目申请审核成功");
    }

    //获取所有项目 分页查询
    @GetMapping("/allproject")
    public AjaxResponse getAllProject(
            Integer pageNum,//当前页码
            Integer pageSize,//每页条数
            @RequestParam(required = false)//表示参数不是必须得
            Integer school//学校
    ) {
        PageBean<ProjectOv> list = inProjectService.getAllProject(pageNum, pageSize, school);
        return AjaxResponse.success("项目信息查询成功", list);
    }

    //查询指定项目的申请人信息    //查询指定项目申请人信息,通过审核的信息
    @GetMapping("/projectjoin/{id}")
    public AjaxResponse getProjectJoin(
            @PathVariable Integer id
    ) {
        System.out.println("id==================>" + id);
        return AjaxResponse.success("项目信息查询成功", inProjectService.selectProjectJoin2(id, 1));
    }

    //查询指定项目的申请人信息
    @GetMapping("/projectuserjoin/{id}")
    public AjaxResponse getProjectUserJoin(
            @PathVariable Integer id
    ) {
        return AjaxResponse.success("项目信息查询成功", inProjectService.selectProjectJoin2(id, null));
    }
    ////查询指定学校项目
    //@GetMapping("/schoolproject/{schoolid}")
    //public AjaxResponse schoolproject(Integer id, String code, String schoolname) {
    //    没有使用
    //    return AjaxResponse.success("查询成功");
    //}

    /**
     * 获取当前登录用户申请的所有项目列表以及项目信息
     *
     * @return
     */
    @GetMapping("/queryApplicationProject")
    public AjaxResponse queryApplicationProject() {
        List<ApplicationProjectListDTO> list = inProjectService.queryApplicationProject();
        return AjaxResponse.success("查询成功", list);
    }

    //增加阅读量
    @GetMapping("/lookproject/{id}")
    public AjaxResponse lookproject(@PathVariable Integer id) {
        inProjectService.updeateLookProject(id);
        return AjaxResponse.success("成功");
    }

    //删除项目
    @PostMapping("/delectproject/{id}")
    public AjaxResponse deleteProject(@PathVariable Integer id) {
        log.info("删除项目id:{}", id);
        inProjectService.deleteProject(id);
        return AjaxResponse.success("删除成功");
    }


    //根据项目ID查询查询项目信息
    @GetMapping("/projectById/{id}")
    public AjaxResponse projectById(@PathVariable String id) {
        ProjectOv project = inProjectService.selectProjectById(id);
        log.info("项目信息:{}", project);
        return AjaxResponse.success("查询成功", project);
    }

    //撤销项目申请信息
    @GetMapping("/revoke/{id}")
    public AjaxResponse revoke(@PathVariable Integer id) {
        inProjectService.revoke(id);
        return AjaxResponse.success("撤销成功");
    }

    //查询项目是否已经投递过
    @GetMapping("/isjoin/{id}")
    public AjaxResponse isjoin(@PathVariable Integer id) {
        boolean b = inProjectService.isjoin(id);
        Map<String, Object> map = new HashMap<>();
        map.put("isJoin", b);
        map.put("msg", b ? "已加入" : "未加入");
        return AjaxResponse.success("查询成功", map);
    }

    //根据项目介绍查询项目
    @PostMapping("/queryProjectByIntroduce")
    public AjaxResponse queryProjectByIntroduce(@RequestBody @Validated QueryDataPoJectInfoPram pram) {
        PageData<ProjectOv> list = inProjectService.queryProjectByIntroduce(pram);
        return AjaxResponse.success("查询成功", list);
    }

    //根据技能要求查询项目信息
    @PostMapping("/queryProjectByskillSummary")
    public AjaxResponse queryProjectByskillSummary(@RequestBody @Validated QueryDataPoJectInfoPram pram) {
        PageData<ProjectOv> list = inProjectService.queryProjectByskillSummary(pram);
        return AjaxResponse.success("查询成功", list);
    }

    //项目创建完成接受项目的订阅提示 审核通知
    @PostMapping("/subscribe")
    public AjaxResponse subscribe(@RequestBody @Validated  SubscribeMassageParam req) {
        //项目审核通知
        SubMassageParam sp = new SubMassageParam();
        sp.setProjectId(req.getProjectId());
        req.setPageUrl(sp.getPageUrl());
        inSubscribeMassageService.subscribe(sp);
        return AjaxResponse.success("订阅成功");
    }


    @PostMapping("/byProjectIds")
    public AjaxResponse byProjectIds(@RequestBody @Validated ProjcetIds req) {
        return AjaxResponse.success("查询成功", inProjectService.byProjectIds(req));
    }

    /**
     * 我投递的人才
     * @param req
     * @return
     */
    @PostMapping("/projectDeliveryOliveBranch")
    public AjaxResponse projectDeliveryOliveBranch(@RequestBody @Validated projectDeliveryOliveBranchParam req) {
        return AjaxResponse.success("查询成功", inProjectService.projectDeliveryOliveBranch(req));
    }


}
